# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=SVT-AV1
pkgbase=mingw-w64-svt-av1
pkgname=("${MINGW_PACKAGE_PREFIX}-svt-av1")
pkgver=1.7.0
pkgrel=1
pkgdesc="Scalable Video Technology AV1 encoder and decoder (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.com/AOMediaCodec/SVT-AV1'
license=('spdx:BSD-3-Clause-Clear')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-yasm"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz")
sha256sums=('ce0973584f1a187aa4abf63f509ff8464397120878e322a3153f87e9c161fc4f')

prepare() {
  cd "${srcdir}"/${_realname}-v${pkgver}
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "${srcdir}"/build-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DREPRODUCIBLE_BUILDS=ON \
      ../${_realname}-v${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  [[ -d "${srcdir}"/build-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DREPRODUCIBLE_BUILDS=ON \
      ../${_realname}-v${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # pkg-config file adds -DEB_DLL in cflags for shared libs only
  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/${_realname}-v${pkgver}/LICENSE.md ${pkgdir}${MINGW_PREFIX}/share/licenses/svt-av1/LICENSE
  install -Dm644 ${srcdir}/${_realname}-v${pkgver}/PATENTS.md ${pkgdir}${MINGW_PREFIX}/share/licenses/svt-av1/PATENTS.md
}
